package tv.higlobal.tcpdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    public EditText editText;
    public TextView textView_send;
    public TextView textView_receive;
    private static final int COMPLETED = 0;
    private static final int COMPLETEDS = 1;
    private static final int COMPLETEDSS = 2;
    private String data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.send_editText);
        textView_send = findViewById(R.id.send_textView);
        textView_receive = findViewById(R.id.receive_textView);
        textView_send.setMovementMethod(ScrollingMovementMethod.getInstance());
        textView_receive.setMovementMethod(ScrollingMovementMethod.getInstance());
        TaskCenter.sharedCenter().setDisconnectedCallback(new TaskCenter.OnServerDisconnectedCallbackBlock() {
            @Override
            public void callback(IOException e) {
                Message message = new Message();
                message.what = COMPLETED;
                handler.sendMessage(message);
               // textView_receive.setText(textView_receive.getText().toString() + "断开连接" + "\n");
            }
        });
        TaskCenter.sharedCenter().setConnectedCallback(new TaskCenter.OnServerConnectedCallbackBlock() {
            @Override
            public void callback() {
                Message message = new Message();
                message.what = COMPLETEDS;
                handler.sendMessage(message);

            }
        });
        TaskCenter.sharedCenter().setReceivedCallback(new TaskCenter.OnReceiveCallbackBlock() {
            @Override
            public void callback(String receicedMessage) {
                Message message = new Message();
                message.what = COMPLETEDSS;
                handler.sendMessage(message);
                data = receicedMessage;
//                textView_receive.setText(textView_receive.getText().toString() + receicedMessage + "\n");
            }
        });
    }

    public void sendMessage(View view) {
        String msg = "0106000100035533";
        textView_send.setText(textView_send.getText().toString() + msg + "\n");

        TaskCenter.sharedCenter().send(CRC16M.getSendBuf(msg));
    }

    public void connect(View view) {
        TaskCenter.sharedCenter().connect("192.168.1.17",26);
    }

    public void disconnect(View view) {
        TaskCenter.sharedCenter().disconnect();
    }

    public void clear1(View view) {
        textView_send.setText("");
    }

    public void clear2(View view) {
        textView_receive.setText("");
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == COMPLETED) {
                textView_receive.setText(textView_receive.getText().toString() + "断开连接" + "\n");
            }else if(msg.what == COMPLETEDS){
                textView_receive.setText(textView_receive.getText().toString() + "连接成功" + "\n");
            }else if(msg.what == COMPLETEDSS){
                textView_receive.setText(textView_receive.getText().toString() + data + "\n");
            }
        }
    };


    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }
}
